import global, { GameRecord } from "../global";
import storage from "../util/storage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Replay extends cc.Component {

    @property(cc.Node)
    Frame_replay:cc.Node = null;     //历史对局框
    
    onLoad(){
        this.Frame_replay.active = false;
    }

    /**
     * 点击复盘按钮
     */
    replay(){
        if(storage.get("record",true)){
            this.Frame_replay.active = true;
            this.Frame_replay.scale = 0;
            cc.tween(this.Frame_replay).to(.2,{scale:1.2}).to(.1,{scale:1}).start();
        }else{
            global.persist.tip("没有历史对局");
        }
    }

    /**
     * 关闭复盘弹框
     */
    close(){
        this.Frame_replay.active = false;
    }

    /**
     * 第一盘
     */
    replayOne(){
        let record:Array<GameRecord> = storage.get("record",true);
        if(record.length>0){
            global.temp.gameRecord = record[record.length-1];
            global.is_replay = true;
            cc.director.loadScene("replay");
        }else{
            global.persist.tip("没有对局记录");;
        }
    }

    /**
     * 第二盘
     */
    replayTwo(){
        let record:Array<GameRecord> = storage.get("record",true);
        if(record.length<2){
            global.persist.tip("没有对局记录");
        }else{
            global.temp.gameRecord = record[record.length-2];
            global.is_replay = true;
            cc.director.loadScene("replay");
        }
    }

    /**
     * 第三盘
     */
    replayThree(){
        let record:Array<GameRecord> = storage.get("record",true);
        if(record.length<3){
            global.persist.tip("没有对局记录");
        }else{
            global.temp.gameRecord = record[0];
            global.is_replay = true;
            cc.director.loadScene("replay");
        }
    }
}
